Skip to content

Conversation

@tarcieri
Copy link
Member

@tarcieri tarcieri commented Jan 18, 2026

Adds a set of workspace-level lints including common ones we've used in the past and others collected via running clippy::pedantic on several of our crates and looking for interesting ones.

Workspace-level lints require explict opt-in from each crate's Cargo.toml, so there's nothing forcing any crate to use them:

[lints]
workspace = true

That said, I have opted every crate in the workspace into these lints and gotten them all to pass except for wycheproof2blb which is an internal utility tool and I didn't feel like it was worth the hassle.

Even then, the lints are all set to "warn", so they can be easily overridden by #[allow(...)] attributes, including #![allow(...)] to shut them off at the granularity of an entire crate.

I managed to fix most of the issues that cargo clippy --fix didn't fix automatically including some missing documentation and documentation formatting issues.

The main thing I didn't fix was adding missing SAFETY comments, which is a problem with a few of the crates in this repo. I disabled a lint at the crate level and added a TODO where we're missing them and it wasn't easily corrected.

@tarcieri tarcieri requested a review from newpavlov January 18, 2026 23:10
@tarcieri tarcieri force-pushed the ci/workspace-level-clippy-config branch from e8b32f8 to 41aa651 Compare January 18, 2026 23:20
@tarcieri
Copy link
Member Author

Oh fun, this seems to be incompatible with the minimal-versions lint since it blows away the workspace-level Cargo.toml

@tarcieri
Copy link
Member Author

tarcieri commented Jan 18, 2026

Not sure what's happening here... what?

error: missing documentation for the crate
  --> blobby/tests/mod.rs:1:1
   |
 1 | / #![cfg(feature = "alloc")]
 2 | | #![allow(missing_docs, clippy::panic_in_result_fn)]
 3 | |
 4 | | const ITEMS_LEN: usize = 10;
...  |
31 | |     Ok(())
32 | | }
   | |_^
   |
   = note: `-D missing-docs` implied by `-D warnings`
   = help: to override `-D warnings` add `#[allow(missing_docs)]`

...it's literally right there!

Edit: though this lint seems fixed easily enough by adding a toplevel rustdoc comment

@tarcieri tarcieri force-pushed the ci/workspace-level-clippy-config branch from 41aa651 to 26fc609 Compare January 18, 2026 23:36
@tarcieri
Copy link
Member Author

Well, it's green, though I had to shut off the minimal-versions checks for the workspace-level config to work.

Maybe we can figure out some way to implement that which doesn't involve blowing away the workspace-level Cargo.toml? That seems a little problematic.

@tarcieri
Copy link
Member Author

Something else we could use these configs for is setting some allows... like needless_range_loop

Adds a set of workspace-level lints including common ones we've used in
the past and others collected via running `clippy::pedantic` on several
of our crates and looking for interesting ones.

Workspace-level lints require explict opt-in from each crate's
Cargo.toml, so there's nothing forcing any crate to use them:

    [lints]
    workspace = true

That said, I have opted every crate in the workspace into these lints
and gotten them all to pass except for `wycheproof2blb` which is an
internal utility tool and I didn't feel like it was worth the hassle.

Even then, the lints are all set to `"warn"`, so they can be easily
overridden by `#[allow(...)]` attributes, including `#![allow(...)]`
to shut them off at the granularity of an entire crate.

I managed to fix most of the issues that `cargo clippy --fix` didn't fix
automatically including some missing documentation and documentation
formatting issues.

The main thing I didn't fix was adding missing `SAFETY` comments,
which is a problem with a few of the crates in this repo. I disabled a
lint at the crate level and added a TODO where we're missing them and it
wasn't easily corrected.
@tarcieri tarcieri force-pushed the ci/workspace-level-clippy-config branch from 26fc609 to fd1cf9d Compare January 19, 2026 02:55
Copy link
Member

@newpavlov newpavlov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you overuse #[must_use] a fair bit and it should be used in cases where a genuine mistake is likely and can cause silent bugs. See: https://std-dev-guide.rust-lang.org/policy/must-use.html As an example, Vec::len is not annotated with it, but you added it to InOutBuf::len.

Since it's not relevant to fixing the new lints, it's probably better to do in a separate PR.


impl std::fmt::Display for CaseResult {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
impl core::fmt::Display for CaseResult {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add use core::fmt; to make it a bit less cluttered?

#![allow(
clippy::missing_safety_doc,
clippy::std_instead_of_alloc,
clippy::std_instead_of_core,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

std_instead_of_core and std_instead_of_alloc could be easily fixed.

clippy::std_instead_of_alloc,
clippy::std_instead_of_core,
clippy::undocumented_unsafe_blocks,
clippy::unwrap_used
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clippy has option allow-unwrap-in-tests which may be better than allowing this lint in every test.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aha, interesting

/// Transform buffer into [`PaddedInOutBuf`] using padding algorithm `P`.
///
/// # Errors
/// - if the padding is invalid
Copy link
Member

@newpavlov newpavlov Jan 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's better to not have one entry lists and write simply:

/// # Errors
/// If the padding is invalid.

Same for other one-entry error and panic docs.


impl Padding for Pkcs7 {
#[inline]
#[allow(clippy::cast_possible_truncation)]
Copy link
Member

@newpavlov newpavlov Jan 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replace the cast with u8::try_from(..).expect("...")? I think the compiler should be able to remove the panic, but it's worth to check.

must_use_candidate = "warn"
needless_range_loop = "allow"
implicit_saturating_sub = "warn"
panic_in_result_fn = "warn"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure about usefulness of the panic_in_result_fn lint in our context.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I marked a couple of my crates as clippy::panic in this PR. panic_in_result_fn is a bit more manageable. It also won't trip on const asserts.

I'm fine with removing it from the workspace-level, but it does seem good to me. At one point I was trying my best to write panic-free code but the lack of tooling to ensure that's actually the case is somewhat demotivating.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIUC the lint did not result in any fixes in our code and I can not recall a single case where it could've been legitimate, so I think it's better to remove it and associated allows.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can remove it, but the general idea that something which returns Result probably shouldn't panic if possible seems like a good one to me

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally, sure. But in practice we use Result for cases when user is responsible for the error and panics to guard against potential bugs in our code. Most of such panics get eliminated by optimization passes, but Clippy is unable to see it.

@newpavlov
Copy link
Member

The minimal versions problem can be probably fixed by tweaking the action to cut [lints] workspace = true from the crate's Cargo.toml.

@tarcieri
Copy link
Member Author

I think you overuse #[must_use] a fair bit and it should be used in cases where a genuine mistake is likely and can cause silent bugs. See: https://std-dev-guide.rust-lang.org/policy/must-use.html As an example, Vec::len is not annotated with it, but you added it to InOutBuf::len.

The changes were applied with cargo clippy --fix. This is what the clippy::must_use_candidate lint thinks should have #[must_use], not me. And the same can be said about most of the fixes in this PR besides the documentation.

FWIW, I was sent down this whole road after encountering a bug in ctutils that would've been caught by #[must_use]. I think we should have some kind of automated check for it, although clippy seemingly has two different checks, and clippy::must_use_candidate seems like the more eager one.

@tarcieri
Copy link
Member Author

The minimal versions problem can be probably fixed by tweaking the action to cut [lints] workspace = true from the crate's Cargo.toml.

I think it would probably be better if it emitted a dummy workspace-level Cargo.toml that just had:

[workspace]
members = ["crate-under-test"]

That way we don't need to update anything if we add any other workspace-level configs.

@newpavlov
Copy link
Member

newpavlov commented Jan 19, 2026

I think it would probably be better if it emitted a dummy workspace-level Cargo.toml

Sure, but it would be a bit harder to implement than just cutting the lines. I think it's fine to start with the simple workaround and improve it later.

The changes were applied with cargo clippy --fix.

Ah, ok. Personally, I think that most of the added #[must_use]s are not necessary, but it's not like they break anything so I am fine with keeping them if you prefer to have them.

@tarcieri
Copy link
Member Author

tarcieri commented Jan 19, 2026

Really? One's just emitting a file from a template where the other you're trying to munge an existing file. Former sure seems easier and more maintainable to me, especially since it will work in perpetuity with other workspace-level configs

@newpavlov
Copy link
Member

newpavlov commented Jan 19, 2026

For emitting the file you need to retrieve the crate name, while cutting the lines is one simple command. We also probably want to keep any other settings in the root Cargo.toml (i.e. ideally we should only modify the members field). I am fine with either option. Either way, I would prefer if the minimal versions jobs were fixed before merging.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants